//
//  MeViewController.swift
//  BaseSwift
//
//  Created by ghwang on 2019/7/22.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit
import EasyNavigation

class MeViewController: UIViewController {

    let iconBtn = UIButton()
    let phoneLab = UILabel()
    var waveView: WavesView?

    lazy var headerView:UIView = {
        
        let bg = UIView()
        bg.backgroundColor = UIColor.white
        bg.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height:200)
        

        
        let headerView = UIView()
        headerView.backgroundColor = UIColor.btnMainColor
        headerView.frame = CGRect(x: 0, y: 0, width: KScreenWidth, height: 200)
        bg.addSubview(headerView)
        self.waveView = WavesView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 200))
        bg.addSubview(self.waveView!)
        self.waveView?.startWaveAnimation()
        
        


        let cancelBtn = UIButton()



        cancelBtn.setImage(UIImage(named: "cancel_white"), for: .normal)
        bg.addSubview(cancelBtn)
        cancelBtn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        let _ = cancelBtn.sd_layout()?.widthIs(24)?.heightIs(24)?.leftSpaceToView(bg,10)?.topSpaceToView(bg,HT_StatusBarHeight+5)
        
        let setBtn = UIButton()
        setBtn.setImage(UIImage(named: "set_white"), for: .normal)
        bg.addSubview(setBtn)
        setBtn.addTarget(self, action: #selector(setClick), for: .touchUpInside)
        let _ = setBtn.sd_layout()?.widthIs(35)?.heightIs(35)?.rightSpaceToView(bg,10)?.topSpaceToView(bg,HT_StatusBarHeight+5)
        
        iconBtn.addTarget(self, action: #selector(infoClick), for: .touchUpInside)
        bg.addSubview(iconBtn)
        let _ = iconBtn.sd_layout()?.widthIs(50)?.heightIs(50)?.topSpaceToView(bg,HT_StatusBarAndNavigationBarHeight+5)?.leftSpaceToView(bg,40)
        iconBtn.sd_cornerRadius = 25
        iconBtn.setImage(UIImage(named: "默认头像"), for: .normal)

        
        bg.addSubview(phoneLab)
        phoneLab.textColor = UIColor.textTitleColorBlack
        phoneLab.font = UIFont.titleFont
        let _ = phoneLab.sd_layout()?.leftSpaceToView(iconBtn,20)?.topSpaceToView(bg,HT_StatusBarAndNavigationBarHeight+5)?.heightIs(40)
        phoneLab.setSingleLineAutoResizeWithMaxWidth(188)
        phoneLab.text = SPUtil.init().nickName
        
        let imgicon = UIImageView()
        imgicon.image = UIImage(named: "me_right")
        self.phoneLab.addSubview(imgicon)
        let _ = imgicon.sd_layout()?.widthIs(8)!.heightIs(8)!.rightSpaceToView(self.phoneLab,-20)!.centerYEqualToView(self.phoneLab)
        
    

        return bg
        
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if SPUtil.init().headImg.count > 0{
              
              iconBtn.sd_setImage(with: URL(string: SPUtil.init().headImg ?? ""), for: .normal) { (A, B, C, D) in
                  
                  
              }
        }else{
            iconBtn.setImage(UIImage(named: "默认头像"), for: .normal)

        }
        if SPUtil.init().nickName.count == 0{
            phoneLab.text = "昵称"
        }else{
            phoneLab.text = SPUtil.init().nickName

        }
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.backgroundColor
        
        self.view.addSubview(headerView)

        //菜单
        let containerView = UIView()
        self.view.addSubview(containerView)
        containerView.backgroundColor = UIColor.white
        var temp = Array<UIView>()
        let tits = ["全部订单","待付款","待出行","待评论"]
        
        for item in tits {
            
            let menuBtn = UIButton()
            menuBtn.setTitle(item, for: .normal)
            menuBtn.titleLabel?.font = UIFont.commonFontSmaller
            menuBtn.titleLabel?.textAlignment = .center
            menuBtn.setImage(UIImage(named: item), for: .normal)
            menuBtn.setTitleColor(UIColor.textTitleColor, for: .normal)
            menuBtn.addTarget(self, action: #selector(menuClick), for: .touchUpInside)
            containerView.addSubview(menuBtn)
            let _ = menuBtn.sd_layout()?.heightIs(115)
            
            let _ = menuBtn.imageView?.sd_layout()?.widthIs(30)?.heightIs(30)?.topSpaceToView(menuBtn,28)?.centerXEqualToView(menuBtn)
            
            let _ = menuBtn.titleLabel?.sd_layout()?.heightIs(15)?.topSpaceToView(menuBtn.imageView,10)?.centerXEqualToView(menuBtn)?.widthRatioToView(menuBtn,1)
            
            
            temp.append(menuBtn)
            
        }
        let _ = containerView.sd_layout()?.topSpaceToView(self.headerView,0)?.widthRatioToView(self.view,1)
        
        containerView.setupAutoWidthFlowItems(temp, withPerRowItemsCount: 4, verticalMargin: 0, horizontalMargin: 0, verticalEdgeInset: 0, horizontalEdgeInset: 0)
        
        self.createCellContent()
        
        
    }
    func createCellContent(){
        

        let containerView = UIView()
        view.addSubview(containerView)
        containerView.backgroundColor = UIColor.white
        var temp = Array<UIView>()
        let tits = ["收藏","帖子","咨询"]
        
        for item in tits {
            
            let menuBtn = UIButton()
            menuBtn.setTitle(item, for: .normal)
            menuBtn.titleLabel?.font = UIFont.commonFont
            menuBtn.setImage(UIImage(named: item), for: .normal)
            
            if item == "帖子" {
                
                menuBtn.setImage(UIImage(named: "评论"), for: .normal)

            }
            menuBtn.setTitleColor(UIColor.textTitleColorBlack, for: .normal)
            menuBtn.addTarget(self, action: #selector(maskClick), for: .touchUpInside)
            containerView.addSubview(menuBtn)
            let _ = menuBtn.sd_layout()?.heightIs(50)
            
            let _ = menuBtn.imageView?.sd_layout()?.widthIs(20)?.heightIs(20)?.centerYEqualToView(menuBtn)?.leftSpaceToView(menuBtn,20)
            
            let _ = menuBtn.titleLabel?.sd_layout()?.heightIs(20)?.leftSpaceToView(menuBtn.imageView,15)?.widthIs(95)
            
            let line = UIView()
            menuBtn.addSubview(line)
            line.backgroundColor = UIColor.lineColor
            let _ = line.sd_layout()?.leftSpaceToView(menuBtn.imageView,20)?.heightIs(1)?.bottomSpaceToView(menuBtn,0)?.rightSpaceToView(menuBtn,0)
            
            let ricon = UIImageView()
            ricon.image = UIImage(named: "向右")
            menuBtn.addSubview(ricon)
            let _ = ricon.sd_layout()?.widthIs(15)?.heightIs(15)?.rightSpaceToView(menuBtn,10)?.centerYEqualToView(menuBtn)
            
            
            temp.append(menuBtn)
            
        }
        let _ = containerView.sd_layout()?.topSpaceToView(view,310+15)?.widthRatioToView(view,1)
        
        containerView.setupAutoWidthFlowItems(temp, withPerRowItemsCount: 1, verticalMargin: 0, horizontalMargin: 0, verticalEdgeInset: 0, horizontalEdgeInset: 0)
        
        
    }
    
    @objc func cancelClick(){POP(vc: self)}
    @objc func infoClick(){
        
        PUSH(from: self, to: PersonInfoVC())
    }
    @objc func setClick(){
        
        PUSH(from: self, to: SetingViewController())
    }
    @objc func menuClick(btn:UIButton){
        
        var index = 0
        if btn.titleLabel?.text == "待付款" {

            index = 1

        }else if btn.titleLabel?.text == "待出行" {

            index = 2

        }else if btn.titleLabel?.text == "待评论" {

            index = 3

        }
        
        let vc = OrderViewController()
        vc.index = index
        PUSH(from: self, to: vc)
        
    }
    @objc func maskClick(btn:UIButton){
        
        if btn.titleLabel?.text == "收藏" {
            
            PUSH(from: self, to: favListViewController())
            
        }else if btn.titleLabel?.text == "帖子" {
            
            let vc = SocialViewController()
            vc.type = "me"
            PUSH(from: self, to: vc)
            
        }else if btn.titleLabel?.text == "咨询" {
            
            PUSH(from: self, to: ZiXunListVC())
        }
        
    }

}
